using System;
using System.Collections.Generic;
using System.Web.UI;
using Soveryn.Commerce.API;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Pages
{
    public partial class CustomerLogin : SSLPageBaseClass
    {

        protected new void Page_PreInit(object sender, EventArgs e)
        {
            Website = WebsiteData.WebsiteDetail();
            PageID = Website.CustomerLoginTemplatePageID.Value;
            base.Page_PreInit(sender, e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {

                if (isLoggedIn && Request.QueryString["Mode"] != null)
                {
                    Response.Redirect("~/" + Request.QueryString["Mode"], true);
                }


                if (Request["email"] != null)  //validated email
                    UserNameEmailTextBox.Text = Request["email"].ToString().Trim();
            }

        }

        protected void EmailPasswordLinkButton_Click(object sender, EventArgs e)
        {
            string errorMessage = "There seems to be a problem emailing your password!  Please double check the spelling of your email address and try again.";
            try
            {

                APIEnvelope env = Soveryn.Commerce.Shared.API.Call(CustomerAPIURL, "CustomerEmailPassword", new
                {
                    WebsiteAPIKey = WebsiteAPIKey,
                    UserNameEmail = UserNameEmailTextBox.Text.Trim()
                }, false, false);


                if (!(bool)env.response)
                    EventMessagePanelLiteral.Text = errorMessage;
                else
                    EventMessagePanelLiteral.Text = "Your password has been be emailed!";

                ForgotPWPanel.Visible = false;
            }
            catch (Exception ex)
            {
                MiscUtils.ErrorLog(ex, errorMessage);
                EventMessagePanelLiteral.Text = errorMessage;
            }
            finally
            {
                EventMessagePanel.Visible = true;
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            // Attempt to Validate User Credentials
            // -----------------------------------
            Boolean Success = false;
            string CustomerEmailAddress = UserNameEmailTextBox.Text.Trim();
            string CustomerPassword = PasswordTextBox.Text.Trim();

            EventMessagePanel.Visible = false;
            ForgotPWPanel.Visible = false;

            APIEnvelope env = Soveryn.Commerce.Shared.API.Call(CustomerAPIURL, "CustomerLogin", new
            {
                WebsiteAPIKey = WebsiteAPIKey,
                CustomerCartSessionID = CustomerCartSessionID,
                UserNameEmail = CustomerEmailAddress,
                Password = CustomerPassword
            }, false, false);

            Success = (bool)env.response;
            if (Success == true)
            {
                if (Request.QueryString["Mode"] != null)
                {
                    Response.Redirect("~/" + Request.QueryString["Mode"], true);
                }
                else
                {
                    Response.Redirect("/OrderCheckoutPROC.aspx", true);
                }
            }
            else
            {
                EventMessagePanelLiteral.Text = "Your username or password is incorrect.  To have your password emailed to you, click the forgot password link below.";
                EventMessagePanel.Visible = true;
                ForgotPWPanel.Visible = true;
            }
        }

    }
}